package com.cpi.cornpp.purchaseinstructiondetail.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.product.exception.ProductException;
import com.cpi.cornpp.product.view.ProductView;
import com.cpi.cornpp.purchaseinstructiondetail.converter.InstructionPurchaseDetailConverter;
import com.cpi.cornpp.purchaseinstructiondetail.dao.InstructionPurchaseDetailDAO;
import com.cpi.cornpp.purchaseinstructiondetail.dto.InstructionPurchaseDetailDTO;
import com.cpi.cornpp.purchaseinstructiondetail.exception.InstructionPurchaseDetailException;
import com.cpi.cornpp.purchaseinstructiondetail.view.InstructionPurchaseDetailView;

public class InstructionPurchaseDetailService {

	/**
	 * Devuelve la lista de compromisos de compra
	 * @param purchaseInstructionId
	 * @return
	 * @throws InstructionPurchaseDetailException
	 */
	public List list(int purchaseInstructionId) throws InstructionPurchaseDetailException{
		InstructionPurchaseDetailDAO dao = null;
		ArrayList list = null;
		ArrayList aux = null;
		InstructionPurchaseDetailConverter converter = null;
		
		dao = new InstructionPurchaseDetailDAO();
		list = new ArrayList();
		aux = new ArrayList();
		converter = new InstructionPurchaseDetailConverter();
		try{
			aux = (ArrayList)dao.getPurchaseInstructionDetails(purchaseInstructionId);
			//aux = (ArrayList)help();
			list = (ArrayList)converter.convertDTOsToViews(aux);		
			
		} catch (InstructionPurchaseDetailException instructionPurchaseDetailException) {			
			instructionPurchaseDetailException.printStackTrace(System.out);
			instructionPurchaseDetailException = new InstructionPurchaseDetailException(instructionPurchaseDetailException, CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw instructionPurchaseDetailException;
		} catch (Exception e){
			InstructionPurchaseDetailException instructionPurchaseDetailException = null;
			e.printStackTrace(System.out);
			instructionPurchaseDetailException = new InstructionPurchaseDetailException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw instructionPurchaseDetailException;
		}	
		return list;
	}
	
	/**
	 * Obtiene un {@link InstructionPurchaseDetailView} basado en un ID
	 * @param int purchaseInstructionDetailId
	 * @return InstructionPurchaseDetailView
	 * @throws InstructionPurchaseDetailException
	 */
	public InstructionPurchaseDetailView getById(int purchaseInstructionDetailId) throws InstructionPurchaseDetailException, ProductException{
		InstructionPurchaseDetailDTO dto = null;
		InstructionPurchaseDetailView view = null;
		InstructionPurchaseDetailDAO dao = null;
		InstructionPurchaseDetailConverter converter = null;
		
		dao = new InstructionPurchaseDetailDAO();
		converter = new InstructionPurchaseDetailConverter();
		try {
			dto= dao.getById(purchaseInstructionDetailId);
			view = converter.converterPurchaseInstructionDetailDTOToView(dto);
		}catch (InstructionPurchaseDetailException instructionPurchaseDetailException) {			
			instructionPurchaseDetailException.printStackTrace(System.out);
			instructionPurchaseDetailException = new InstructionPurchaseDetailException(instructionPurchaseDetailException, CornPPException.LAYER_SERVICE,CornPPException.ACTION_SELECT);
			throw instructionPurchaseDetailException;
		}catch( ProductException productException ){
			throw productException;
		} catch (Exception e){
			InstructionPurchaseDetailException instructionPurchaseDetailException = null;
			e.printStackTrace(System.out);
			instructionPurchaseDetailException = new InstructionPurchaseDetailException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_SELECT);
			throw instructionPurchaseDetailException;
		}	
		return view;		
	}
	
	/**
	 * Actualiza un {@link InstructionPurchaseDetailView}
	 * @param view
	 * @throws InstructionPurchaseDetailException
	 */
	public void update(InstructionPurchaseDetailView view) throws InstructionPurchaseDetailException{
		InstructionPurchaseDetailDTO dto = null;		
		InstructionPurchaseDetailDAO dao = null;
		InstructionPurchaseDetailConverter converter = null;
		
		dao = new InstructionPurchaseDetailDAO();
		converter = new InstructionPurchaseDetailConverter();
		try {
			dto = converter.converterPurchaseInstructionDetailViewToDto(view);
			dao.update(dto);			
		}catch (InstructionPurchaseDetailException instructionPurchaseDetailException) {			
			instructionPurchaseDetailException.printStackTrace(System.out);
			instructionPurchaseDetailException = new InstructionPurchaseDetailException(instructionPurchaseDetailException, CornPPException.LAYER_SERVICE,CornPPException.ACTION_UPDATE);
			throw instructionPurchaseDetailException;
		} catch (Exception e){
			InstructionPurchaseDetailException instructionPurchaseDetailException = null;
			e.printStackTrace(System.out);
			instructionPurchaseDetailException = new InstructionPurchaseDetailException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_UPDATE);
			throw instructionPurchaseDetailException;
		}
		
	}
	
	/**
	 * Guarda un {@link InstructionPurchaseDetailView}
	 * @param view
	 * @throws InstructionPurchaseDetailException
	 */
	public void save(InstructionPurchaseDetailView view) throws InstructionPurchaseDetailException{
		InstructionPurchaseDetailDTO dto = null;		
		InstructionPurchaseDetailDAO dao = null;
		InstructionPurchaseDetailConverter converter = null;
		Iterator productsIterator = null;
		ProductView productView = null;
		dao = new InstructionPurchaseDetailDAO();
		converter = new InstructionPurchaseDetailConverter();
		try {
			dto = converter.converterPurchaseInstructionDetailViewToDto(view);
			dao.insert(dto);
			view.setPurchaseInstructionDetailid(dto.getPurchaseInstructionDetailid());
			productsIterator = view.getProductList().iterator();
			while(productsIterator.hasNext()){
				productView = (ProductView)productsIterator.next();
				dao.insertItemDetail(view.getPurchaseInstructionDetailid(), productView.getProductId());
			}
		}catch (InstructionPurchaseDetailException instructionPurchaseDetailException) {			
			instructionPurchaseDetailException.printStackTrace(System.out);
			instructionPurchaseDetailException = new InstructionPurchaseDetailException(instructionPurchaseDetailException, CornPPException.LAYER_SERVICE,CornPPException.ACTION_UPDATE);
			throw instructionPurchaseDetailException;
		} catch (Exception e){
			InstructionPurchaseDetailException instructionPurchaseDetailException = null;
			e.printStackTrace(System.out);
			instructionPurchaseDetailException = new InstructionPurchaseDetailException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_UPDATE);
			throw instructionPurchaseDetailException;
		}
		
	}
}
